package com.fei.controller;

import com.fei.bean.AdminBean;
import com.fei.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
public class AdminController {

    Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    AdminService adminService;

    @RequestMapping("/")
    public String tologin(){
        return "login";
    }

    @RequestMapping("toregister")
    public String toRegister(){
        return "register";
    }

    //未授权页面
    @RequestMapping("/toUnauthorized")
    public String toUnauthorized(){
        return "unauthorized";
    }

    //注销
    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }


//    验证管理员名字
    @RequestMapping("validateName")
    @ResponseBody
    public Boolean validateName(String name){
        return adminService.validateNameService(name);
    }

    //    验证管理员手机号
    @RequestMapping("validatePhone")
    @ResponseBody
    public Boolean validatePhone(String phone){
        return adminService.validatePhoneService(phone);
    }


    @RequestMapping("register")
    public String Register(@Valid AdminBean adminBean,
                           BindingResult result){
        if (result.hasErrors()){
            System.out.println(result.getAllErrors());
            return "register";
        }
        adminService.registerAdminService(adminBean);
        return "login";
    }



    @RequestMapping("admin/toIndex")
    public String index(Model model){
        List<Integer> list = adminService.selectCount();
        model.addAttribute("count",list);
        return "index";
    }

    @RequestMapping("login")
    public String login(AdminBean adminBean,Model model){
        //获取当前管理员
        Subject subject = SecurityUtils.getSubject();
        //封装管理员的登陆数据
        UsernamePasswordToken token = new UsernamePasswordToken(adminBean.getName(), adminBean.getPassword());

        try {
            subject.login(token);//执行登录方法
            List<Integer> list = adminService.selectCount();
            model.addAttribute("count",list);
            return "index";
        } catch (UnknownAccountException e) {//没有这名管理员
            model.addAttribute("msg","没有这个管理员");
            return "login";
        }catch (IncorrectCredentialsException e) {//密码错误
            model.addAttribute("msg","密码错误");
            return "login";
        }


    }


}
